package com.atguigu.gmall.mq.controller;

import com.atguigu.gmall.common.rabbit.service.RabbitService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: 小兰
 * @Date: 2023/5/8 19:35
 */
@RestController
@RequestMapping("/admin/mq")
public class MqController {
    @Autowired
    private RabbitService rabbitService;
    /**
     * 發送普通消息
     * @param msg
     * @return
     */
    @GetMapping("/sendMsg")
    public Result sendMsg(@RequestParam("msg")  String msg) {
        String exchange = "exchange.confirm";
        String routingKey = "routing.confirm";
            rabbitService.sendMsg(exchange,routingKey,msg);
            return Result.ok();
    }

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 采用死信实现延迟消息
     * @param msg
     * @return
     */
    @GetMapping("/sendDeadLetterMsg")
    public Result sendDeadLetterMsg(@RequestParam("msg") String msg){
        rabbitTemplate.convertAndSend(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,msg);
        return  Result.ok();

    }

    /**
     * 基于延迟插件实现延迟消息
     * @param msg
     * @param ttl
     * @return
     */
    @GetMapping("/sendDelayMsg")
    public Result sendDelayMsg(@RequestParam("msg") String msg, @RequestParam("ttl")Integer ttl){
        rabbitService.sendDelyMsg(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,msg,ttl);
        return Result.ok();
    }
}
